package com.ruoyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.system.domain.vo.ArchiveTaskVo;
import com.ruoyi.system.domain.bo.ArchiveTaskBo;
import com.ruoyi.system.service.IArchiveTaskService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 归档任务控制器
 * 前端访问路由地址为:/system/task
 *
 * @author ruoyi
 * @date 2024-12-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/task")
public class ArchiveTaskController extends BaseController {

    private final IArchiveTaskService iArchiveTaskService;

    /**
     * 查询归档任务列表
     */
    @SaCheckPermission("system:task:list")
    @GetMapping("/list")
    public TableDataInfo<ArchiveTaskVo> list(ArchiveTaskBo bo, PageQuery pageQuery) {
        return iArchiveTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出归档任务列表
     */
    @SaCheckPermission("system:task:export")
    @Log(title = "归档任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArchiveTaskBo bo, HttpServletResponse response) {
        List<ArchiveTaskVo> list = iArchiveTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "归档任务", ArchiveTaskVo.class, response);
    }

    /**
     * 获取归档任务详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:task:query")
    @GetMapping("/{id}")
    public R<ArchiveTaskVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iArchiveTaskService.queryById(id));
    }

    /**
     * 新增归档任务
     */
    @SaCheckPermission("system:task:add")
    @Log(title = "归档任务", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArchiveTaskBo bo) {
        return toAjax(iArchiveTaskService.insertByBo(bo));
    }

    /**
     * 修改归档任务
     */
    @SaCheckPermission("system:task:edit")
    @Log(title = "归档任务", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArchiveTaskBo bo) {
        return toAjax(iArchiveTaskService.updateByBo(bo));
    }

    /**
     * 删除归档任务
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:task:remove")
    @Log(title = "归档任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iArchiveTaskService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
